define(function(require,exports,module){
	require("resource/js/easyui/themes/icon.css");
	require("oa/act/approval/form/css/form.css");
	require("resource/js/easyui/easyloader.js");
	var ajax = require("resource/js/component/ajax.js");
	var easyutil = require("resource/js/component/easyutil.js");
	var pageNo = 1,pageSize = 8;
	
	function initPage(){
		waitApprovalData(true,1,8);
		initClick();
	}
	function initClick(){
		$("#waitApproval").unbind('click');
		$("#waitApproval").bind('click',function(){
			waitApprovalData(true,1,8);
			$("#waitApprovalList").show();
			$("#approvalList").hide();
			$("#waitApproval").attr('class','selected');
			$("#approval").attr('class','');
		});
		$("#approval").unbind('click');
		$("#approval").on('click',function(){
			approvalData(true,1,8);
			$("#waitApprovalList").hide();
			$("#approvalList").show();
			$("#waitApproval").attr('class','');
			$("#approval").attr('class','selected');
		});
	}
	
	var colorNumber = {
			0 : "orange",
			1 : "green",
			2 : "purple",
			3 : "red",
			4 : "grey"
	}
	//待审核
	var waitApprovalData = function(flag,pageNo,pageSize){
		if(flag){
			$("#waitApprovalList ul").text("");
		}
		var option ={
			    "pageNo":pageNo,
			    "pageSize":pageSize
		    }
		ajax.init().success(function(code,msg,data){
			if(code==0){
				if(data){
					$("#noData").hide();
					for(var k in data.rows){
						var abstractStr = data.rows[k]['abstract'] || '';
						if(!data.rows[k].businessName){
							data.rows[k].businessName='流程已被删除';
						}
						$("<li><div><span class="+colorNumber[data.rows[k].status]+">"+data.rows[k].statustext+"</span></div>" +
							 "<div>"+workBeanchTrim(data.rows[k].businessName)+"</div>" +
							 "<div><xmp>"+contentNotempty(parseAbstract(abstractStr)&&parseAbstract(abstractStr)[0])+"</xmp></div>" +
							 "<div>发起人："+workBeanchTrim(data.rows[k].createUser)+"</div>" +
							  "<div>发起时间："+workBeanchTrim(data.rows[k].startTime)+"</div></li>").appendTo($("#waitApprovalList ul")).each(function(){
									$.data(this,{data:data.rows[k]});
								}).on("click",function(){
									$(this).addClass("selected").siblings().removeClass("selected");
						    	  $(".jgui-drawer-right").removeClass("jgui-drawer-close").addClass("jgui-drawer-open");
						    	  openDetailsApproval($.data(this).data);
						      });
					}
					$("#waitApprovalList").find(".query-more").remove();
					if(data.rows&&data.rows.length>=pageSize && data.total>8){
						$("<div class='query-more'><a>查看更多</a></div>").appendTo($("#waitApprovalList")).on("click",function(){
							pageNo++;
							waitApprovalData(false,pageNo,pageSize);
						});
					}else{
						if(data.total>8){
							$("<div class='query-more'><a style='cursor: default;'>没有更多数据了!</a></div>").appendTo($("#waitApprovalList"));
						}
					}
				}else{
					$("#noData").show();
					$("#noData").find(".textTip").text("没有待我审批的");
				}
			}
		}).post(BASEPATH + "/oa/act/activitiWaitMatter/getWaitDoneByPaged.htm",option);
	}
	
	//内容不能为空，超过30字符显示......
	var contentNotempty =function(content){
		if(content){
			if(content.toString().length>30){
				return content.toString().substring(0,30)+'......';
			}else if(content.toString().length<=30){
				return content.toString().substring(0,30);
			}
		}else{
			return "";
		}
	}
	
	//完结和终止的时候，下级审批人显示完结或者终止
	var showEmptyEnd = function(content,startTime,endTime){
		if(content){
			return "下级审批人：" + content;
		}else if(startTime){
			if(endTime){
				return "结束时间："+ endTime;
			}else{
				return "发起时间："+ startTime;
			}
		}
	}
	
	//待审核审批详情
	var openDetailsApproval = function(row){
		var option ={
			    "businessId":row.businessId
		    }
		ajax.init().success(function(code,msg,data){
			if(data.waitDoneCount > 0){
				$('#remindersFile').hide();	
				$("#jgui-title").text('我的审批-待审核（'+ workBeanchTrim(row.businessName)+'）');
				$(".duty_drawer").show();
				var backDlg = require("resource/js/component/backDialog");
				var baseUrl = BASEPATH + "/oa/act/activitiMain/getProcessPage.htm";
				var urlPath=baseUrl+"?key="+row.processKey+"&processInstanceId="+row.processInstanceId+"&taskId="+row.taskId;
				$("#myiframe").attr("src",urlPath);
				$(".jgui-drawer-right").removeClass("jgui-drawer-close").addClass("jgui-drawer-open");
				//打印
				$("#printFile").unbind("click");
				$("#printFile").on("click",function(){
					printFile(row.businessId);
				});
			}
		}).post(BASEPATH + "/oa/act/activitiWaitMatter/queryWaitDoneByPageCount.htm",option);
	} 
	function parseAbstract(abstractStr){
		var separator = "@VITO@";
		if(abstractStr){
			return abstractStr.split(separator);
		}
	}
	//已审核
	var approvalData = function(flag,pageNo,pageSize){
		if(flag){
			$("#approvalList ul").text("");
			$("#approvalList").find(".query-more").remove();
		}
		var option ={
			    "pageNo":pageNo,
			    "pageSize":pageSize,
			    "putDetail":true
		    }
		ajax.init().success(function(code,msg,data){
			if(code==0){
				if(data.rows){
					$("#noData").hide();
					for(var k in data.rows){
						var abstractStr = data.rows[k]['abstract'] || '';
						if(!data.rows[k].processname){
							data.rows[k].processname="流程已被删除"
						}
						$("<li><div><span class="+colorNumber[data.rows[k].status]+">"+data.rows[k].statustext+"</span></div>" +
								"<div>"+workBeanchTrim(data.rows[k].processname)+"</div>" +
								"<div>"+contentNotempty(parseAbstract(abstractStr)&&parseAbstract(abstractStr)[0])+"</div>" +
								"<div>发起人："+workBeanchTrim(data.rows[k].username)+"</div>" +
								"<div style='width:20%;'>"+showEmptyEnd(data.rows[k].assigneename,data.rows[k].starttime)+"</div>" +
								"<div>"+showEmptyEnd(null,data.rows[k].starttime,data.rows[k].endtime)+"</div></li>").appendTo($("#approvalList ul")).each(function(){
									$.data(this,{data:data.rows[k]});
								}).on("click",function(){
									$(this).addClass("selected").siblings().removeClass("selected");
									openDetails($.data(this).data);
								});
					}
					$("#approvalList").find(".query-more").remove();
					if(data.rows&&data.rows.length>=pageSize && data.total>8){
						$("<div class='query-more'><a>查看更多</a></div>").appendTo($("#approvalList")).on("click",function(){
							pageNo++;
							approvalData(false,pageNo,pageSize);
						});
					}else{
						if(data.total>8){
							$("<div class='query-more'><a style='cursor: default;'>没有更多数据了!</a></div>").appendTo($("#approvalList"));
						}
					}
				}else{
					$("#noData").show();
					$("#noData").find(".textTip").text("没有我已审批的");
				}
			}
		}).post(BASEPATH + "/oa/act/actExApproveInfo/queryApprove.htm",option);
	}
	
	//已审核查看详情
	var openDetails = function(row,flag){
		$('#remindersFile').hide();
		if(flag){
			$("#approval").click();
			approvalData(true,1,8);
			$("#waitApprovalList").hide();
			$("#approvalList").show();
			$("#waitApproval").attr('class','');
			$("#approval").attr('class','selected');
			flag = false;
		}	
		$(".jgui-drawer-right").removeClass("jgui-drawer-close").addClass("jgui-drawer-open");
		$(".duty_drawer").show();
		$("#jgui-title").text('我的审批-已审核（'+ workBeanchTrim(row.processname)+'）');
		var backDlg = require("resource/js/component/backDialog");
//		if(row.path && row.path.length > 0){
//			var urlPath=BASEPATH+"/view/oa/act/main/actProcessHistory?url="+row["path"]+"&bussId=" + row["businessid"];	
//		}else{
			var urlPath = BASEPATH + "/oa/act/activitiMain/getProcessViewPage.htm?key="+row.processkey+"&bussId="+row["businessid"];
//		}
		$("#myiframe").attr("src",urlPath);
		
		//打印
		$("#printFile").unbind("click");
		$("#printFile").on("click",function(){
			printFile(row.businessid);
		});
		if(row.assignee){
			//催办
			$('#remindersFile').show();
			$("#remindersFile").unbind("click")
		    $("#remindersFile").on("click",function(){
		    	ajax.init().success(function(code,msg,data){
					if(code==0){
						$(".jgui-drawer-right").find("span.close").click();
						approvalData(true,1,8);
					}
					$.messager.show({ title:'系统提示', msg:msg, timeout:5000, showType:'slide' });
				}).post(BASEPATH + "/oa/act/actExApproveInfo/doSendMsg.htm",{"processkey" : row.processkey,"checkeds" : row.assignee})
		    });
		}
	}
	
	var printFile = function(businessid){
		window.parent.$(".oa-maskAll").show();
		ajax.init().success(function(code,msg,data){
			window.parent.$(".oa-maskAll").hide();
			var param=data.filePath+data.fileId+"."+data.fileExt;
			if(code==0){
				$.messager.confirm("系统提示",msg,function(r){
					if(r){
						window.open(BASEPATH+"/download/file/"+param);
					}
				});
				
			}
		}).error(function(code,msg,data){
			window.parent.$(".oa-maskAll").hide();
			if(msg!="")
				$.messager.show('系统提示',msg);
		}).post(BASEPATH+"/oa/act/print/printprocess.htm",{"businessId":businessid});
	}
	
	//关闭详情
	$(".jgui-drawer-right").find(".close").click(function(){
		$(".jgui-drawer-right").addClass("jgui-drawer-close").removeClass("jgui-drawer-open");
	});
	
	var workBeanchTrim = function(res){
		return res || "";
	}
	
	module.exports={
			openDetail:function(row,callback){
				return openDetails(row,true);
			},
			openDetailApproval:function(row,callback){
				return openDetailsApproval(row,true);
			},			
			trans:function(){
				using(["parser","textbox","numberbox","dialog","combobox"],function(){
					initPage();
				});
			}
	}
	
});
